package dominio.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dominio.controlador.CtrlPrincipal;

public class NavegacaoServlet extends HttpServlet {

	private static final long serialVersionUID = 4825615609286455462L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String acao = request.getParameter("acao") == null ? "" : request.getParameter("acao");
		
		if (acao.equals("") || acao.equals("homepage")) {
			CtrlPrincipal ctrlPrincipal = new CtrlPrincipal(request, response, getServletContext());
			request.setAttribute("OMaisAssitido", ctrlPrincipal.carregarFilmeMaisAssitido());
			request.setAttribute("EmDestaque", ctrlPrincipal.carregarFilmeEmDestaque());
			request.setAttribute("ListaFilmesAssistidosPorRanking", ctrlPrincipal.carregarListaFilmesPorRanking());
			request.setAttribute("ListaFilmesAssistidosPorData", ctrlPrincipal.carregarListaFilmesPorData());
			request.setAttribute("ListaGenero", ctrlPrincipal.carregarListaGenero());
			
			String nextJSP = "/estrutura.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request,response);
		} else if (acao.equals("detalharFilme")) {
			int id = Integer.parseInt(request.getParameter("id"));
			
			CtrlPrincipal ctrlPrincipal = new CtrlPrincipal(request, response, getServletContext());
			request.setAttribute("OMaisAssitido", ctrlPrincipal.carregarFilmeMaisAssitido());
			request.setAttribute("EmDestaque", ctrlPrincipal.carregarFilmeEmDestaque());
			request.setAttribute("ListaFilmesAssistidosPorRanking", ctrlPrincipal.carregarListaFilmesPorRanking());
			request.setAttribute("ListaFilmesAssistidosPorData", ctrlPrincipal.carregarListaFilmesPorData());
			request.setAttribute("ListaGenero", ctrlPrincipal.carregarListaGenero());
			request.setAttribute("Filme", ctrlPrincipal.carregarFilme(id));
			
			String nextJSP = "/paginas/filme/e_filme.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request,response);
		} else if (acao.equals("cadastro")) {
			CtrlPrincipal ctrlPrincipal = new CtrlPrincipal(request, response, getServletContext());
			request.setAttribute("OMaisAssitido", ctrlPrincipal.carregarFilmeMaisAssitido());
			request.setAttribute("EmDestaque", ctrlPrincipal.carregarFilmeEmDestaque());
			request.setAttribute("ListaFilmesAssistidosPorRanking", ctrlPrincipal.carregarListaFilmesPorRanking());
			request.setAttribute("ListaFilmesAssistidosPorData", ctrlPrincipal.carregarListaFilmesPorData());
			request.setAttribute("ListaGenero", ctrlPrincipal.carregarListaGenero());
			
			String nextJSP = "/paginas/cadastro/e_cadastro.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request,response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String acao = request.getParameter("acao");
		
		if (acao.equals("pesquisa")) {
			String titulo = request.getParameter("titulo");
			int genero = Integer.parseInt(request.getParameter("genero"));
			
			CtrlPrincipal ctrlPrincipal = new CtrlPrincipal(request, response, getServletContext());
			request.setAttribute("ListaGenero", ctrlPrincipal.carregarListaGenero());
			request.setAttribute("ListaFilmes", ctrlPrincipal.carregarListaFilmesPorCriterio(titulo, genero));
			request.setAttribute("OMaisAssitido", ctrlPrincipal.carregarFilmeMaisAssitido());
			request.setAttribute("EmDestaque", ctrlPrincipal.carregarFilmeEmDestaque());
			
			String nextJSP = "/paginas/pesquisa/e_pesquisa.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request,response);
		} else if (acao.equals("cadastrar")) {
			String nome = request.getParameter("nome");
			String sobrenome = request.getParameter("sobrenome");
			String email = request.getParameter("email");
			String login = request.getParameter("login");
			String senha = request.getParameter("senha");
			String csenha = request.getParameter("csenha");
			
			CtrlPrincipal ctrlPrincipal = new CtrlPrincipal(request, response, getServletContext());
			ctrlPrincipal.gravarNovoUsuario(nome, sobrenome, email, login, senha, csenha);
		}
	}

}
